﻿using DevExpress.XtraEditors;
using DevExpress.XtraPrinting;
using Lamex.Base;
using Lamex.Common;
using Lamex.Library;
using Lamex.Library.CommonClass;
using Lamex.mMES.BLL;
using Lamex.mMES.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mMES.UI
{
    public partial class FrmMES_TestMGT : frmBaseDataForm
    {
        Lamex.mMES.ucControls.ucShowPic ucShowPicList = null;
        Image myBmp;//图片定义
        Point mouseDownPoint = new Point(); //记录拖拽过程鼠标位置
        bool isMove = false;    //判断鼠标在picturebox上移动时，是否处于拖拽过程(鼠标左键是否按下)
        int zoomStep = 75;      //缩放步长
        byte[] buffer = null;
        ResourceManager rm = null;
        string[] NoSendMail = { "罗元盛", "何应书", "李钟哇" };
        public FrmMES_TestMGT()
        {
            InitializeComponent();
            ShowSummary();
            GetOwnerList();
        }

        private void FrmMES_TestMGT_Load(object sender, EventArgs e)
        {
            try { this.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
        }
        private void ShowSummary()
        {
            xtraTabPage2.PageEnabled = false;
            xtraTabPage1.PageEnabled = true;
            lookUpEdit2.Properties.ValueMember = "Module_Menu";
            lookUpEdit2.Properties.DisplayMember = "Module_Menu";
            lookUpEdit2.Properties.DataSource = getModule_Menu();
            //lookUpEdit2.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            lookUpEdit2.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            lookUpEdit2.Properties.ImmediatePopup = true;
            lookUpEdit2.Properties.NullText = "选择模块或菜单";
        }
        public DataTable getModule_Menu()
        {
            string sql = @"SELECT * FROM (
SELECT A.Name+S.Name+' '+C.Name as Module_Menu
 FROM   (SELECT ID,Name FROM Sys_Menu WHERE PID='-1') A 
 LEFT JOIN Sys_Menu S ON A.ID=S.PID 
 LEFT JOIN Sys_Menu C ON S.ID=C.PID 
 UNION 
 SELECT A.Name+S.Name as Module_Menu
 FROM   (SELECT ID,Name FROM Sys_Menu WHERE PID='-1') A 
 LEFT JOIN Sys_Menu S ON A.ID=S.PID  
 UNION
 select 'A_测试管理-VBA'
  UNION
 select 'A_测试管理-NC'
  UNION
 select 'A_测试管理-IE'
  UNION
 select 'A_测试管理-MES'
) B WHERE B.Module_Menu IS NOT NULL
 ORDER BY B.Module_Menu";
            DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
            return dt;
        }
        public void getLookUpEditValue()
        {
            lookUpEdit1.Properties.ValueMember = "Module_Menu";
            lookUpEdit1.Properties.DisplayMember = "Module_Menu";
            lookUpEdit1.Properties.DataSource = getModule_Menu();
            lookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            lookUpEdit1.Properties.ImmediatePopup = true;
            lookUpEdit1.Properties.NullText = "选择模块或菜单";
        }
        public void GetOwnerList()
        {
            string sql = @"SELECT USERNAME FROM [Lamex_Project_Manager].[dbo].[MUSERINFO]
where DEPTID='MES'
order by USERID";
            DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                uctxtOwner.Properties.Items.Add(dt.Rows[i]["USERNAME"].ToString());
                cbOwner.Properties.Items.Add(dt.Rows[i]["USERNAME"].ToString());
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        public override void DoView(Lamex.Interface.IButtonInfo sender)
        {
            string sqlWhere = "";
            string sql = " select IDNO,[Module_Menu],[Desc_Question],[Reply_Idea],[Owner],[C_User],[C_TS],[Update_User],[Update_TS],[Flag] from Sys_TestManage_Record where 1=1 ";
            if (!string.IsNullOrEmpty(uctxtFlag.Text))
            {
                sqlWhere += " and Flag like '%" + uctxtFlag.Text + "%'";
            }
            if (!string.IsNullOrEmpty(uctxtOwner.Text))
            {
                sqlWhere += " and Owner like '%" + uctxtOwner.Text + "%'";
            }
            if (this.lookUpEdit2.EditValue != null && this.lookUpEdit2.EditValue != "")
            {
                sqlWhere += " and Module_Menu like '%" + lookUpEdit2.Text + "%'";
            }

            sql = sql + sqlWhere;
            DataTable dt = BaseFactory<Sys_TestManage_Record>.Instance.GetDataTableBySql(sql);
            //DoBindingSummaryGrid(dt);
            gridControl1.DataSource = dt;
            gridView1.BestFitColumns();
            base.DoView(sender);
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        public override void DoAdd(Lamex.Interface.IButtonInfo sender)
        {
            if (!BuildSqlHelper.IsExistList("Sys_TestManage_Record", "LamexMESManager")) return;
            xtraTabPage1.PageEnabled = false;
            xtraTabPage2.PageEnabled = true;
            xtraTabPage2.PageVisible = true;
            txtReply.Enabled = false;
            cbFlag.Enabled = false;
            btnAutoPic.Enabled = true;
            txtDesc.ReadOnly = false;
            lookUpEdit1.Enabled = true;
            cbOwner.Enabled = true;
            cbFlag.Text = "New";
            IsEdit = "Add";
            xtraTabPage2.Tag = "新增";
            lookUpEdit1.Text = "";
            txtReply.Text = "";
            txtDesc.Text = "";        
            //pictureEdit1.Image = null;
            panelControl3.Controls.Clear();
            cbOwner.Text = "";
            getLookUpEditValue();
            //lookUpEdit1.Properties.ValueMember = "Module_Menu";
            //lookUpEdit1.Properties.DisplayMember = "Module_Menu";
            //lookUpEdit1.Properties.DataSource = getModule_Menu();
            //lookUpEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.Standard;
            //lookUpEdit1.Properties.ImmediatePopup = true;
            //lookUpEdit1.Properties.NullText = "选择模块或菜单";
            
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        public override void DoEdit(Lamex.Interface.IButtonInfo sender)
        {
            #region 按钮权限
           // if (!BuildSqlHelper.IsExistList("Sys_TestManage_Record")) return;
            #endregion
            if (gridView1.SelectedRowsCount == 0)
            {
                if (Loginer.CurrentUser.ChangeLanguage == 0)
                    DevExpress.XtraEditors.XtraMessageBox.Show("【没有选中要修改的数据！】", "提示", MessageBoxButtons.OK);
                else
                    DevExpress.XtraEditors.XtraMessageBox.Show("【No Choose Modify Data ！】", "提示", MessageBoxButtons.OK);
                return;
            }
            int m = gridView1.FocusedRowHandle;
            DataRow dr = gridView1.GetDataRow(m);
            KeysName = dr["IDNO"].ToString();

            xtraTabPage1.PageEnabled = false;
            xtraTabPage2.PageEnabled = true;
            xtraTabPage2.PageVisible = true;
            txtReply.Enabled = true;
            cbFlag.Enabled = true;
            btnAutoPic.Enabled = false;
            txtDesc.ReadOnly = true;
            lookUpEdit1.Enabled = false;
            cbOwner.Enabled = false;
            cbFlag.Text = "";
            IsEdit = "Edit";
            xtraTabPage2.Tag = "编辑";         
          

            //***********************加载数据****************************//



            getLookUpEditValue();
            lookUpEdit1.EditValue = dr["Module_Menu"].ToString();
            cbOwner.Text = dr["Owner"].ToString();
            txtDesc.Text = dr["Desc_Question"].ToString();
            txtReply.Text = dr["Reply_Idea"].ToString();
            lookUpEdit1.Text = lookUpEdit1.EditValue.ToString();
            //string pic_Temp = dr["REF_Picture"].ToString();
            //byte[] pic_Temp = System.Convert.FromBase64String(dr["REF_Picture"].ToString());  
            //单独获取图片
            string sql_Pic = @"select REF_Picture from Sys_TestManage_Record where IDNO='"+KeysName+"'";
            DataTable dt = BaseBuildSql.GetDataTableBySql(sql_Pic);
            byte[] pic_Temp = (byte[])dt.Rows[0]["REF_Picture"];
            if (pic_Temp.Length > 0)
            {
               // pictureEdit1.Image = byteArrayToImage(pic_Temp);
               // myBmp = byteArrayToImage(pic_Temp);
                panelControl3.Controls.Clear();
                ucShowPicList = new ucControls.ucShowPic();
                ucShowPicList.LoadImageList(byteArrayToImage(pic_Temp));
                ucShowPicList.Dock = DockStyle.Fill;
                panelControl3.Controls.Add(ucShowPicList);
            }

           // base.DoEdit(sender);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        public override void DoSave(Lamex.Interface.IButtonInfo sender)
        {
            if (!BuildSqlHelper.IsExistList("Sys_TestManage_Record", "LamexMESManager")) return;
           
            if (IsEdit == "Add")
            {
                //Sys_TestManage_RecordInfo model = new Sys_TestManage_RecordInfo();
                //model.Module_Menu = txtModule_Menu.Text;
                //model.Owner = cbOwner.Text;
                //model.Desc_Question = txtDesc.Text;
                //model.REF_Picture = buffer;
                //model.Reply_Idea = txtReply.Text;
                //model.C_TS = DateTime.Now;
                //model.C_User = Lamex.Common.Loginer.CurrentUser.AccountName;
                //model.Flag = cbFlag.Text;
                //model.Update_TS = DateTime.Now;
                //model.Update_User = "";
                if (this.lookUpEdit1.EditValue == null || this.lookUpEdit1.EditValue == "")
                {
                    MessageBox.Show("模块或菜单不允许为空！");
                    return;
                }
                if (string.IsNullOrEmpty(cbOwner.Text))
                {
                    MessageBox.Show("负责人不允许为空！");
                    return;
                }
                if (string.IsNullOrEmpty(txtDesc.Text))
                {
                    MessageBox.Show("问题描述不允许为空！");
                    return;
                }
                string strModule = lookUpEdit1.Text;
                string strOwner = cbOwner.Text;
                string strDesc_Question = txtDesc.Text;
                byte[] strREF_Picture = buffer;
                string strC_User = Lamex.Common.Loginer.CurrentUser.AccountName;
                string strFlag = cbFlag.Text;
                string Insert_Sql = "";
                //获取负责人邮箱
                string mail_Sql = @" select top 1 Email from Sys_MyUser where UserName='" + cbOwner.Text + "'";
                DataTable dtmail = BaseBuildSql.GetDataTableBySql(mail_Sql);
                int m = 0;
                if (strREF_Picture != null)
                {
                    Insert_Sql = @"insert into Sys_TestManage_Record (Module_Menu,Desc_Question,REF_Picture,Owner,C_User,C_TS,Flag) values";
                    Insert_Sql = Insert_Sql + " ('" + strModule + "',@Desc_Question,@image,'" + strOwner + "','" + strC_User + "',getdate(),'" + strFlag + "')";
                    m = Write(Insert_Sql, strDesc_Question, buffer);
                }
                else
                {
                    Insert_Sql = @"insert into Sys_TestManage_Record (Module_Menu,Desc_Question,REF_Picture,Owner,C_User,C_TS,Flag) values";
                    Insert_Sql = Insert_Sql + " ('" + strModule + "',@Desc_Question,'','" + strOwner + "','" + strC_User + "',getdate(),'" + strFlag + "')";
                    //m = BaseBuildSql.ExecuteNonQuery(Insert_Sql);
                    m = Write1(Insert_Sql, strDesc_Question);
                }

                if (m > 0)
                {
                    //发送邮件
                    if (!NoSendMail.Contains(strC_User))
                    {
                        if (dtmail.Rows.Count > 0)
                            SendMailUseGmail_Add(dtmail.Rows[0]["Email"].ToString(), strOwner, strModule, strDesc_Question, strC_User, DateTime.Now);
                    }
                    if (Loginer.CurrentUser.ChangeLanguage == 0)
                        DevExpress.XtraEditors.XtraMessageBox.Show("新增成功", "提示", MessageBoxButtons.OK);
                    else
                        DevExpress.XtraEditors.XtraMessageBox.Show("Add Successed", "Tips", MessageBoxButtons.OK);

                }
                else
                {
                    if (Loginer.CurrentUser.ChangeLanguage == 0)
                        DevExpress.XtraEditors.XtraMessageBox.Show("新增失败", "提示", MessageBoxButtons.OK);
                    else
                        DevExpress.XtraEditors.XtraMessageBox.Show("Add Failed", "Tips", MessageBoxButtons.OK);
                }

            }
            if (IsEdit == "Edit")
            {
                if (string.IsNullOrEmpty(cbFlag.Text))
                {
                    MessageBox.Show("问题进度不允许为空！");
                    return;
                }
                int IDNO = int.Parse(KeysName);
                string Update_Sql = @"update [LamexMESManager].[dbo].[Sys_TestManage_Record] set Update_User='" + Lamex.Common.Loginer.CurrentUser.AccountName + "',Update_TS=GETDATE(),Flag='" + cbFlag.Text + "',Reply_Idea='" + txtReply.Text + "'";
                Update_Sql = Update_Sql + " where IDNO='" + IDNO + "'";
                int m = BaseBuildSql.ExecuteNonQuery(Update_Sql);
                string sql = @"select * from [LamexMESManager].[dbo].[Sys_TestManage_Record] where IDNO='" + IDNO + "'";
                DataTable dtTemp = BaseBuildSql.GetDataTableBySql(sql);
                string mail_Sql = @" select top 1 Email from Sys_MyUser where UserName='" + dtTemp.Rows[0]["C_User"].ToString() + "'";
                DataTable dtmail = BaseBuildSql.GetDataTableBySql(mail_Sql);
                if (m > 0)
                {
                    //发送邮件
                    //if (dtmail.Rows.Count > 0 && cbFlag.Text == "Fixed")
                    //    SendMailUseGmail_Edit(dtmail.Rows[0]["Email"].ToString(), dtTemp.Rows[0]["C_User"].ToString(), lookUpEdit1.Text, txtDesc.Text, dtTemp.Rows[0]["C_User"].ToString(), Convert.ToDateTime(dtTemp.Rows[0]["C_TS"].ToString()), txtReply.Text, Lamex.Common.Loginer.CurrentUser.AccountName, DateTime.Now);
                    //发送邮件                  
                    if (!NoSendMail.Contains(dtTemp.Rows[0]["C_User"].ToString()))
                    {
                        if (dtmail.Rows.Count > 0)
                            SendMailUseGmail_Edit(dtmail.Rows[0]["Email"].ToString(), dtTemp.Rows[0]["C_User"].ToString(), lookUpEdit1.Text, txtDesc.Text, dtTemp.Rows[0]["C_User"].ToString(), Convert.ToDateTime(dtTemp.Rows[0]["C_TS"].ToString()), txtReply.Text, Lamex.Common.Loginer.CurrentUser.AccountName, DateTime.Now);
                    }
                    if (Loginer.CurrentUser.ChangeLanguage == 0)
                        DevExpress.XtraEditors.XtraMessageBox.Show("修改成功", "提示", MessageBoxButtons.OK);
                    else
                        DevExpress.XtraEditors.XtraMessageBox.Show("Edit Successed", "Tips", MessageBoxButtons.OK);

                }
                else
                {
                    if (Loginer.CurrentUser.ChangeLanguage == 0)
                        DevExpress.XtraEditors.XtraMessageBox.Show("修改失败", "提示", MessageBoxButtons.OK);
                    else
                        DevExpress.XtraEditors.XtraMessageBox.Show("Edit Failed", "Tips", MessageBoxButtons.OK);
                }
            }

            xtraTabPage2.PageEnabled = false;
            xtraTabPage1.PageEnabled = true;
            DoView(null);
            //base.DoSave(sender);

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        public override void DoDelete(Lamex.Interface.IButtonInfo sender)
        {
            if (!BuildSqlHelper.IsExistList("Sys_TestManage_Record", "LamexMESManager")) return;
            if (gridView1.SelectedRowsCount == 0)
            {
                if (Loginer.CurrentUser.ChangeLanguage == 0)
                    MessageBox.Show("【没有选中要删除的数据，请选择好数据！】");
                else
                    MessageBox.Show("【No Choose Delete Data,Please Choose Data】");

                return;
            }
            int m = gridView1.FocusedRowHandle;
            DataRow dr = gridView1.GetDataRow(m);
            if (dr != null)
            {
                if (BaseFactory<Sys_TestManage_Record>.Instance.Delete(dr["IDNO"].ToString()))
                {
                    if (Loginer.CurrentUser.ChangeLanguage == 0)
                        MessageBox.Show("【数据删除成功！】");
                    else
                        MessageBox.Show("【Data Remove Succesed！】");

                }
                else
                {
                    if (Loginer.CurrentUser.ChangeLanguage == 0)
                        MessageBox.Show("【数据删除失败！】");
                    else
                        MessageBox.Show("【Data Remove failed！】");

                }
            }
            DoView(null);
            base.DoDelete(sender);
        }
        //点击获取剪贴板内容
        private void btnAutoPic_Click(object sender, EventArgs e)
        {
            //首先清空panelControl3控件
            IDataObject iData = Clipboard.GetDataObject();

            //if (iData.GetDataPresent(DataFormats.Text))
            //{
            //    string a = ((String)iData.GetData(DataFormats.Text));
            //}
            if (iData.GetDataPresent(DataFormats.Bitmap))
            {
                string path = "d://Clipboard_temp.jpg";
                //显示剪贴板图片
                Image img = (Bitmap)iData.GetData(DataFormats.Bitmap);
                //pictureEdit1.Image = img;
                buffer = PhotoImageInsert(img);
                panelControl3.Controls.Clear();
                ucShowPicList = new ucControls.ucShowPic();
                ucShowPicList.LoadImageList(img);
                ucShowPicList.Dock = DockStyle.Fill;
                panelControl3.Controls.Add(ucShowPicList);

            }
        }
        //把picturebox的Image 转换成二进制
        public byte[] PhotoImageInsert(System.Drawing.Image imgPhoto)
        {
            //将Image转换成流数据，并保存为byte[] 
            MemoryStream mstream = new MemoryStream();
            imgPhoto.Save(mstream, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] byData = new Byte[mstream.Length];
            mstream.Position = 0;
            mstream.Read(byData, 0, byData.Length);
            mstream.Close();
            return byData;
        }

        /// <summary>
        /// 根据图片路径返回图片的字节流byte[]
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <returns>返回的字节流</returns>
        private static byte[] getImageByte(string imagePath)
        {
            //FileStream files = new FileStream(imagePath, FileMode.Open);
            //byte[] imgByte = new byte[files.Length];
            //files.Read(imgByte, 0, imgByte.Length);
            //files.Close();
            //return imgByte;
            FileStream fs = new FileStream(imagePath, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);
            Byte[] byData = br.ReadBytes((int)fs.Length);
            fs.Close();
            return byData;
        }
        /// <summary>
        /// 字节数组生成图片
        /// </summary>
        /// <param name="Bytes">字节数组</param>
        /// <returns>图片</returns>
        private Image byteArrayToImage(byte[] Bytes)
        {
            MemoryStream ms = new MemoryStream(Bytes);
            return Bitmap.FromStream(ms, true);
        }
    
        //插入数据库-因有图片类型，故需重新inser语句
        private int Write(string strSql, string Desc_Question, byte[] imageBytes)
        {
            string sConn = SqlConfiguration.CurrentConnectionString;

            using (SqlConnection conn = new SqlConnection(sConn))
            {
                using (SqlCommand cmd = new SqlCommand(strSql, conn))
                {
                    try
                    {
                        conn.Open();                   
                        //SqlParameter sqlParameter = new SqlParameter("@image", SqlDbType.Image);
                        //sqlParameter.Value = imageBytes;
                        //cmd.Parameters.Add(sqlParameter);
                        //int rows = cmd.ExecuteNonQuery();
                        cmd.Parameters.Add(new SqlParameter("@Desc_Question", SqlDbType.NVarChar, 500));
                        cmd.Parameters.Add(new SqlParameter("@image", SqlDbType.Image));
                        cmd.Parameters["@Desc_Question"].Value = Desc_Question;
                        cmd.Parameters["@image"].Value = imageBytes;
                        int rows = cmd.ExecuteNonQuery();
                        conn.Close();
                        return rows;
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }
        }
        //后续改为通用防sql注入
        private int Write1(string strSql, string Desc_Question)
        {
            string sConn = SqlConfiguration.CurrentConnectionString;

            using (SqlConnection conn = new SqlConnection(sConn))
            {
                using (SqlCommand cmd = new SqlCommand(strSql, conn))
                {
                    try
                    {
                        conn.Open();
                        cmd.Parameters.Add(new SqlParameter("@Desc_Question", SqlDbType.NVarChar, 500));                   
                        cmd.Parameters["@Desc_Question"].Value = Desc_Question;                      
                        int rows = cmd.ExecuteNonQuery();
                        conn.Close();
                        return rows;
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
            }
        }
        //发送邮件-新增
        public void SendMailUseGmail_Add(string strMailAddress, string strName, string strModule, string strDesc, string strCuser, DateTime strTs)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            // msg.To.Add("Bill.Luo@lamex.com.hk");
            msg.To.Add(strMailAddress);


            msg.CC.Add("Rocky.He@Lamex.com.hk");
            msg.CC.Add("XiangXiang.Lv@lamex.com.hk");

            msg.From = new MailAddress("mailsender@lamex.com.hk", "admin", System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址（可以随便写），发件人姓名，编码*/

            msg.Subject = "MES系统-测试问题跟进提醒";//邮件标题    
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码    
            // msg.Body = "Dear ALL  良率报表详见附件，请查收！";//邮件内容    
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码    
            msg.IsBodyHtml = true;//是否是HTML邮件    
            msg.Priority = MailPriority.Normal;//邮件优先级    
            string strBody = "  ";
            string strTitle = "";
            string strHtml = "";
            string[] strFiled = new string[10];

            strTitle = "<H4>" + strName + "<H4>";
            strTitle = strTitle + "您好,有如下问题需要你跟进处理\r\n";


            strFiled[0] = strFiled[0] + " <td style=width:500px;>" + strModule + "</td> ";
            strFiled[1] = strFiled[1] + " <td style=width:500px;>" + strDesc + "</td> ";
            strFiled[2] = strFiled[2] + " <td style=width:500px;>" + strCuser + "</td> ";
            strFiled[3] = strFiled[3] + " <td style=width:500px;>" + strTs + "</td> ";

            strBody = "";
            //strBody = strBody + " 您好,以下申请超过2天未审批, \r\n";
            strBody = strBody + strTitle;
            strBody = strBody + " ";
            strBody = strBody + strHtml;
            strBody = strBody + ",谢谢！";
            strBody = strBody + " <table  border=8 style=background-color:#CCE8EB;font-size:small;align:right> ";
            strBody = strBody + " <tr><td style=width:150px;>模块或菜单:        </td>" + strFiled[0] + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>问题描述:    </td>" + strFiled[1] + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>提出人:</td>" + strFiled[2] + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>提出时间:</td>" + strFiled[3] + "</tr> ";
            strBody = strBody + " </table> ";
            msg.Body = strBody;


            SmtpClient client = new SmtpClient();
            // client.Credentials = new System.Net.NetworkCredential("luoyuansheng@moptron.biz", "Luo7701167");
            client.Credentials = new System.Net.NetworkCredential("mailsender@lamex.com.hk", "Bl@7701167//");
            //上述写你的GMail邮箱和密码    
            //client.Port = 25;//Gmail使用的端口  25  
            client.Host = "172.23.134.118";
            // client.EnableSsl = true;//经过ssl加密    
            object userState = msg;
            try
            {
                // client.SendAsync(msg, userState);
                client.Send(msg);
                GC.Collect();

            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.Message, "发送邮件出错");
            }
        }
        //发送邮件-修改
        public void SendMailUseGmail_Edit(string strMailAddress, string strName, string strModule, string strDesc, string strCuser, DateTime strTs,string strReply,string strUpdateUser,DateTime strUpdateTs)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            // msg.To.Add("Bill.Luo@lamex.com.hk");
            msg.To.Add(strMailAddress);


            msg.CC.Add("Rocky.He@Lamex.com.hk");
            msg.CC.Add("XiangXiang.Lv@lamex.com.hk");

            msg.From = new MailAddress("mailsender@lamex.com.hk", "admin", System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址（可以随便写），发件人姓名，编码*/

            msg.Subject = "MES系统-测试问题完成反馈";//邮件标题    
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码    
            // msg.Body = "Dear ALL  良率报表详见附件，请查收！";//邮件内容    
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码    
            msg.IsBodyHtml = true;//是否是HTML邮件    
            msg.Priority = MailPriority.Normal;//邮件优先级    
            string strBody = "  ";
            string strTitle = "";
            string strHtml = "";
            string[] strFiled = new string[10];

            strTitle = "<H4>" + strName + "<H4>";
            strTitle = strTitle + "您好,如下测试问题已完成，请知悉\r\n";


            strFiled[0] = strFiled[0] + " <td style=width:500px;>" + strModule + "</td> ";
            strFiled[1] = strFiled[1] + " <td style=width:500px;>" + strDesc + "</td> ";
            strFiled[2] = strFiled[2] + " <td style=width:500px;>" + strCuser + "</td> ";
            strFiled[3] = strFiled[3] + " <td style=width:500px;>" + strTs + "</td> ";
            strFiled[4] = strFiled[4] + " <td style=width:500px;>" + strReply + "</td> ";
            strFiled[5] = strFiled[5] + " <td style=width:500px;>" + strUpdateUser + "</td> ";
            strFiled[6] = strFiled[6] + " <td style=width:500px;>" + strUpdateTs + "</td> ";

            strBody = "";
            //strBody = strBody + " 您好,以下申请超过2天未审批, \r\n";
            strBody = strBody + strTitle;
            strBody = strBody + " ";
            strBody = strBody + strHtml;
            strBody = strBody + ",谢谢！";
            strBody = strBody + " <table  border=8 style=background-color:#CCE8EB;font-size:small;align:right> ";
            strBody = strBody + " <tr><td style=width:150px;>模块或菜单:        </td>" + strFiled[0] + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>问题描述:    </td>" + strFiled[1] + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>提出人:</td>" + strFiled[2] + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>提出时间:</td>" + strFiled[3] + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>处理意见:</td>" + strFiled[4] + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>负责人:</td>" + strFiled[5] + "</tr> ";
            strBody = strBody + " <tr><td style=width:150px;>完成时间:</td>" + strFiled[6] + "</tr> ";
            strBody = strBody + " </table> ";
            msg.Body = strBody;


            SmtpClient client = new SmtpClient();
            // client.Credentials = new System.Net.NetworkCredential("luoyuansheng@moptron.biz", "Luo7701167");
            client.Credentials = new System.Net.NetworkCredential("mailsender@lamex.com.hk", "Bl@7701167//");
            //上述写你的GMail邮箱和密码    
            //client.Port = 25;//Gmail使用的端口  25  
            client.Host = "172.23.134.118";
            // client.EnableSsl = true;//经过ssl加密    
            object userState = msg;
            try
            {
                // client.SendAsync(msg, userState);
                client.Send(msg);
                GC.Collect();

            }
            catch (System.Net.Mail.SmtpException ex)
            {
                MessageBox.Show(ex.Message, "发送邮件出错");
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            DoEdit(null);
        }

      
       

    }
}
